package com.zbkj.crmeb.system.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.github.pagehelper.PageInfo;
import com.utils.PageUtils;
import com.zbkj.crmeb.enums.NoticeStatusEnum;
import com.zbkj.crmeb.merchant.admin.controller.AdminBaseController;
import com.zbkj.crmeb.store.model.CustomsRecord;
import com.zbkj.crmeb.store.request.CustomsRecordSearchRequest;
import com.zbkj.crmeb.store.response.CustomsRecordResponse;
import com.zbkj.crmeb.store.service.CustomsRecordService;
import com.zbkj.crmeb.system.model.Notice;
import com.zbkj.crmeb.system.request.NoticeSearchRequest;
import com.zbkj.crmeb.system.response.NoticeResponse;
import com.zbkj.crmeb.system.service.NoticeService;
import com.zbkj.crmeb.system.service.SystemAdminService;


/**
 * 通知表
 *
 * @author lss
 * @email didiok@foxmail.com
 * @date 2024-07-24 15:12:34
 */
@Slf4j
@RestController
@RequestMapping("api/admin/system/notice")
public class NoticeController extends AdminBaseController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private SystemAdminService systemAdminService;

    /**
     * 分页显示通知表
     *
     * @param request          搜索条件
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<NoticeResponse>> list(@Validated NoticeSearchRequest request, HttpServletRequest servletRequest) {
        log.info("list：{}", JSON.toJSONString(request));
        request.setMerId(getMerId(servletRequest));
        PageInfo<NoticeResponse> pageInfo = noticeService.getList(request);
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 统计商户未读消息
     * @param servletRequest
     * @return
     */
    @GetMapping("/count")
    public CommonResult<Integer> count(HttpServletRequest servletRequest){
        Integer merId = getMerId(servletRequest);
        Integer count = noticeService.lambdaQuery()
                .eq(Notice::getMerId, merId)
                .eq(Notice::getNoticeStatus, 0).count();
        return CommonResult.success(count);
    }

    /**
     * 根据adminId获取通知
     *
     * @param request          搜索条件
     */
    @RequestMapping(value = "/listByAdminId", method = RequestMethod.GET)
    public CommonResult<CommonPage<NoticeResponse>> listByAdminId(@Validated NoticeSearchRequest request, HttpServletRequest servletRequest) {
        log.info("listByAdminId：{}", JSON.toJSONString(request));

        if(request.getAdminId() == null){
            request.setAdminId(systemAdminService.getAdminId());
        }
        PageInfo<NoticeResponse> pageInfo = noticeService.getList(request);
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 查询通知信息
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "详情")
    @GetMapping("/info/{id}")
    public CommonResult<Notice> info(@PathVariable Integer id) {
        log.info("info id：{}", id);
        Notice notice = noticeService.getById(id);
        return CommonResult.success(notice);
    }

    /**
     * 新增通知
     * @author Mr.Zhang
     * @since 2020-05-27
     */
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> save(@RequestBody @Validated Notice notice, HttpServletRequest servletRequest) {
        log.info("save notice：{}", JSON.toJSONString(notice));

        notice.setMerId(getMerId(servletRequest));
        return noticeService.add(notice) ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 已读通知
     * @param id 参数
     * @since 2020-05-27
     */
    @ApiOperation(value = "已读")
    @PutMapping("/read")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> read(Integer id) {
        log.info("read id：{}", id);

        Notice notice = noticeService.getById(id);
        notice.setNoticeStatus(NoticeStatusEnum.READ.getCode());
        boolean update = noticeService.updateById(notice);

        return update ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "删除通知")
    @DeleteMapping("/delete/{id}")
    public CommonResult<String> delete(@PathVariable Integer id) {
        log.info("delete id：{}", id);

        return noticeService.removeById(id) ? CommonResult.success() : CommonResult.failed();
    }

}
